package com.suke.czx.modules.performance.controller;

import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.CodeConstant;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.PerformanceSaveGroup;
import com.suke.czx.common.validate.PerformanceUpdateGroup;
import com.suke.czx.modules.performance.entity.Performance;
import com.suke.czx.modules.performance.entity.PerformanceSearchBo;
import com.suke.czx.modules.performance.service.PerformanceService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 应用模块名称: 绩效
 * 代码描述: 绩效控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/27 2:37
 */
@Validated
@RestController
@RequestMapping("/api/performance")
@AllArgsConstructor
@Api(value = "PerformanceController", tags = "绩效管理")
public class PerformanceController extends AbstractController {

    private final PerformanceService performanceService;

    /**
     * 分页查询绩效列表
     * @param performanceSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:performance:list')")
    public R list(PerformanceSearchBo performanceSearchBo) {
        if (performanceSearchBo.getPage() == null || performanceSearchBo.getLimit() == null) {
            return R.error("缺少分页信息");
        }
        // 分页查询绩效列表
        return performanceService.pagePerformanceList(performanceSearchBo);
    }

    /**
     * 保存绩效
     * @param performance 绩效
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存绩效")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:performance:save')")
    public R save(@Validated(PerformanceSaveGroup.class) Performance performance, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        if (performance.getType() != CodeConstant.PERFORMANCE_INTERNAL_TYPE) {
            return R.error("只能保存内部绩效");
        }
        performance.setCreator(getUserId());
        performance.setCreateTime(new Date());
        performanceService.save(performance);
        return R.ok();
    }

    /**
     * 更新绩效
     * @param performance 绩效
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新绩效")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:performance:update')")
    public R update(@Validated(PerformanceUpdateGroup.class) Performance performance, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        if (performance.getType() != CodeConstant.PERFORMANCE_INTERNAL_TYPE) {
            return R.error("只能更新内部绩效");
        }
        performance.setUpdater(getUserId());
        performance.setUpdateTime(new Date());
        performanceService.updateById(performance);
        return R.ok();
    }

    /**
     * 删除绩效
     * @param performanceId 绩效编码
     * @return VO
     */
    @SysLog("删除绩效")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:performance:delete')")
    public R delete(@NotNull(message = "绩效编码不能为空") Long performanceId) {
        Performance performance = performanceService.getById(performanceId);
        if (performance != null && performance.getType() != CodeConstant.PERFORMANCE_INTERNAL_TYPE) {
            return R.error("只能删除内部绩效");
        }
        return performanceService.updateDelPerformance(performanceId, getUserId());
    }

    /**
     * 导出绩效
     * @param performanceSearchBo 查询BO
     * @return VO
     */
    @SysLog("导出绩效")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:performance:export')")
    public String export(PerformanceSearchBo performanceSearchBo, HttpServletResponse response) {
        performanceService.exportPerformance(performanceSearchBo, response);
        return "success";
    }

}